package com.wut.minio.service.impl;

import cn.hutool.core.date.DatePattern;
import com.wut.minio.common.vo.MinioBucketResponse;
import com.wut.minio.service.BucketService;
import com.wut.minio.utils.MinioUtil;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

@Service
public class BucketServiceImpl implements BucketService {

    private final MinioUtil minioUtil;

    public BucketServiceImpl(MinioUtil minioUtil) {
        this.minioUtil = minioUtil;
    }

    @Override
    public Boolean checkBucketExist(String bucketName) {
        return minioUtil.bucketExist(bucketName);
    }

    @Override
    public List<MinioBucketResponse> getBuckets() {
        return minioUtil.getBuckets().stream()
            .map(bucket -> new MinioBucketResponse(bucket.name(), bucket.creationDate().format(DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_FORMAT.getPattern()))))
            .collect(Collectors.toList());
    }

    @Override
    public Boolean createBucket(String bucketName) {
        return minioUtil.createBucket(bucketName);
    }

    @Override
    public Boolean removeBucket(String bucketName) {
        return minioUtil.removeBucket(bucketName);
    }
}
